#!/usr/bin/env python

from staticpage_class import StaticPage


def create ( liwe, html = '', name = '', title = '', id_page = None, id_html = 0, visible = 1 ):
	"""
	:INPUT:

		- name:			SAFE_STRING
		- title:		SAFE_STRING
		- html:			SAFE_HTML
		- id_page:		SAFE_NUMBER
		- visible:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify
	"""
	sp = StaticPage ( liwe )

	return { 'id_page': sp.create ( html = html, name = name, title = title, id_page = id_page, id_html = id_html, visible = visible ) }

def list ( liwe, _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:

		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	limit = _X_LINES

	sp = StaticPage ( liwe )
	res = sp.list ( start_point, limit )

	count = 0
	for v in res [ 'rows' ]:
		v [ "_pos" ] = start_point + count
		res [ "row%d" % count ] = v

		v [ '_img' ] = ''

		count += 1

	res [ 'rows' ] = res [ 'tot_rows' ]

	res [ 'from_row' ] = start_point
	res [ 'to_row' ] = start_point + count
	res [ 'lines' ] = _X_LINES


	return res

def list_names ( liwe ):
	sp = StaticPage ( liwe )
	res = sp.list ( 0, 0 )

	pages = []
	for p in res.get ( "rows", [] ):
		pages.append ( { "id" : p [ 'id' ], "name" : p [ 'name' ], "title" : p [ 'title' ] } )

	return { "pages" : pages }


def get ( liwe, id_page ):
	"""
	:INPUT:

		- id_page:		SAFE_NUMBER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	sp = StaticPage ( liwe )

	return { 'page': sp.get ( id_page ) }

def delete ( liwe, ids ):
	"""
	:INPUT:

		- ids:		SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify news
	"""

	sp = StaticPage ( liwe )

	return sp.delete ( ids )
